# encoding=utf-8
# 载入模块
import uuid
from loginServer.RedisOper import *


class AuthOper():
    @staticmethod
    def check_module_right(token, product_id, module_id=0,
                           right_content='CRUD'):
        user_id = None
        permit = False
        code = -1
        data = []
        msg = ''
        g_login, g_user_id = RedisOper.get_userid_bytoken(token)
        if g_login:
            print('token验证成功')
            # 1 select sid as role_id from t_user_role where user_id='test'
            #   and product_id ='1'
            # 2 select right_id from t_role_right where role_id in ()
            # 3 select module_id,product_id,right_content from t_right where
            # sid in ('xxx','yyyy')  and product_id =''

            ret = {}
            post_data = {}
            post_data['user_id'] = [g_user_id]
            post_data['product_id'] = [product_id]
            ret = {}  # isel(User_role, ret, post_data, querycolname='user_id', querycolname2 = 'product_id', sort = 'asc')
            roleids = []
            if ret['count'] >= 1:
                for item in ret['data']:
                    roleids.append(item['role_id'])
            else:
                return

            ret = {}
            post_data = {}
            post_data['role_id'] = [roleids]
            ret = {}  # iselin(Role_right, ret, post_data, inquerycolname='role_id',  querycolname2='')
            rightids = []
            if ret['count'] >= 1:
                for item in ret['data']:
                    rightids.append(item['right_id'])
            else:
                return

            ret = {}
            post_data = {}
            post_data['id'] = [rightids]
            post_data['module_id'] = [module_id]
            ret = {}  # iselin(Role_right, ret, post_data, inquerycolname='role_id', querycolname2 = 'module_id')
            if ret['count'] >= 1:
                # ret['data'] = sid,product_id,module_id,right_content
                data = ret['data']
                msg = 'ok'
                code = 0
                permit = True
            else:
                return

        else:
            print('token验证失败')
            return permit, user_id, code, data, msg


@staticmethod
def check_product_right(token, product_id):
    user_id = None
    permit = False
    code = -1
    data = []
    msg = ''
    g_login, g_user_id = RedisOper.get_userid_bytoken(token)
    user_id = g_user_id
    if g_login:
        print('token验证成功')
        ret = {}
        post_data = {}
        post_data['user_id'] = [g_user_id]
        post_data['product_id'] = [product_id]
        ret = {}  # isel(User_role, ret, post_data, querycolname='user_id', querycolname2 = 'product_id', sort = 'asc')
        roleids = []
        if ret['count'] >= 1:
            for item in ret['data']:
                roleids.append(item['role_id'])
        else:
            return

        ret = {}
        post_data = {}
        post_data['role_id'] = [roleids]
        ret = {}  # iselin(Role_right, ret, post_data, inquerycolname='role_id',  querycolname2='')
        rightids = []
        if ret['count'] >= 1:
            for item in ret['data']:
                rightids.append(item['right_id'])
        else:
            return

        ret = {}
        post_data = {}
        post_data['sid'] = [rightids]
        ret = {}  # iselin(Role_right, ret, post_data, inquerycolname='sid',  querycolname2='')
        if ret['count'] >= 1:
            # ret['data'] = sid,product_id,module_id,right_content
            data = ret['data']
            msg = 'ok'
            code = 0
            permit = True
            RedisOper.set_permit_byuseridproductid(user_id, product_id, True)
        else:
            return
    else:
        print('token验证失败')
    return permit, user_id, code, data, msg


@staticmethod
def check_all_right(token):
    user_id = None
    permit = False
    code = -1
    data = []
    msg = ''
    g_login, g_user_id = RedisOper.get_userid_bytoken(token)
    if g_login:
        print('token验证成功')
        ret = {}
        post_data = {}
        post_data['user_id'] = [g_user_id]
        ret = {}  # isel(User_role, ret, post_data, querycolname='user_id', sort = 'asc')
        roleids = []
        if ret['count'] >= 1:
            for item in ret['data']:
                roleids.append(item['role_id'])
        else:
            return

        ret = {}
        post_data = {}
        post_data['role_id'] = [roleids]
        ret = {}  # iselin(Role_right, ret, post_data, inquerycolname='role_id',  querycolname2='')
        rightids = []
        if ret['count'] >= 1:
            for item in ret['data']:
                rightids.append(item['right_id'])
        else:
            return

        ret = {}
        post_data = {}
        post_data['id'] = [rightids]
        ret = {}  # iselin(Role_right, ret, post_data, inquerycolname='role_id', querycolname2='')
        if ret['count'] >= 1:
            # ret['data'] = sid,product_id,module_id,right_content
            data = ret['data']
            msg = 'ok'
            code = 0
            permit = True
        else:
            return
    else:
        print('token验证失败')
    return permit, user_id, code, data, msg
